/**
 * 
 */
package org.xserver.runtime.jetty.command;

import java.util.Properties;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.xserver.annotation.PerformanceLog;
import org.xserver.annotation.VisitorLog;
import org.xserver.core.command.ICommand;

/**
 * @author jkonopka
 * 
 */
public class ConnectorInitializeCommand implements ICommand {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4111848437674963750L;

	/**
	 * 
	 */
	private static final String SERVER_ADDRESS = "server.address";

	/**
	 * 
	 */
	private static final String SERVER_PORT = "server.start.port";

	private final Server server;
	private final Properties properties;

	/**
	 * @param server {@link Server}
	 * @param properties {@link Properties}
	 */
	public ConnectorInitializeCommand(final Server server, final Properties properties) {
		this.server = server;
		this.properties = properties;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xserver.core.command.ICommand#execute()
	 */
	@Override
	@VisitorLog
	@PerformanceLog
	public void execute() throws Exception {
		final Connector connector = new SelectChannelConnector();
		connector.setPort(getPort());
		connector.setHost(getAddress());
		server.addConnector(connector);
	}

	/**
	 * @return
	 */
	private String getAddress() {
		return properties.getProperty(SERVER_ADDRESS);
	}

	/**
	 * @return
	 * @throws NumberFormatException
	 */
	private Integer getPort() throws NumberFormatException {
		return Integer.valueOf(properties.getProperty(SERVER_PORT));
	}

}
